package com.example.gzysmartcity.viewmodel;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ThreadUtils;
import com.example.gzysmartcity.repository.local.SearchListDB;
import com.example.gzysmartcity.repository.model.News;
import com.example.gzysmartcity.repository.model.SCNews;
import com.example.gzysmartcity.repository.model.SearchList;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.utils.NetworkUtil;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class SearchViewModel extends ViewModel {

    private List<SearchList> searchLists;
    private List<SCNews.DataBean> scNewsList;
    private List<News> newsListForSearch;
    private List<News> newsList;
    private final MutableLiveData<State> mutableLiveData = new MutableLiveData<>();

    public void getAllNews() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<SCNews>() {
            @Override
            public SCNews doInBackground() throws Throwable {
                loadAllSearchList();
                return NetworkUtil.getInstance().get("SCNews/getAll?", null, SCNews.class, 0);
            }

            @Override
            public void onSuccess(SCNews result) {
                scNewsList = result.getData();
                newsList = new ArrayList<>();
                newsListForSearch = new ArrayList<>();
                for (SCNews.DataBean a : scNewsList) {
                    Document parse = Jsoup.parse(a.getContent());
                    Elements imgElements = parse.getElementsByTag("img");
                    String imgUrl;
                    if (imgElements.size() == 0)
                        imgUrl = "";
                    else
                        imgUrl = imgElements.get(0).attr("src");
                    String content = parse.getElementsByTag("p").get(0).text();
                    newsListForSearch.add(new News(imgUrl, a.getName(), content, a.getCreateTime(), a.getAuthor(), a.getContent(), a.getId()));
                }
                mutableLiveData.postValue(State.SUCCESS);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
                mutableLiveData.postValue(State.FAIL);
            }
        });
    }

    public LiveData<State> getMutableLiveData() {
        return mutableLiveData;
    }

    public List<SearchList> getSearchLists() {
        return searchLists;
    }

    public int getSearchListSize() {
        return searchLists.size();
    }

    public List<News> getNewsList() {
        return newsList;
    }

    public void search(String name) {
        if (newsListForSearch.size() == 0) {
            mutableLiveData.setValue(State.FAIL);
            return;
        }
        Log.i("TAG", "search: " + name);
        SearchList searchList = new SearchList(name);
        SearchListDB.insert(searchList);
        searchLists.add(searchList);
        newsList.clear();
        newsList.addAll(newsListForSearch.stream().filter(a -> a.getTitle().contains(name)).collect(Collectors.toList()));
    }

    private void loadAllSearchList() {
        searchLists = SearchListDB.loadAll();
    }

    public News getContent(int p) {
        return newsList.get(p);
    }


    public void clear() {
        if (searchLists.size() == 0) {
            return;
        }
        searchLists.clear();
        SearchListDB.clear();
    }
}
